package com.dodock.android.banglapapers;

import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;

public class ComplexScriptHelper {
	
	final static LinkedHashMap<String, String> arrayReplacement = new LinkedHashMap<String, String>();
	
	static{
		//Erfan's English fix
		arrayReplacement.put("a", new String(Character.toChars(0xF200)));
		arrayReplacement.put("A", new String(Character.toChars(0xF200)));
		arrayReplacement.put("b", new String(Character.toChars(0xF201)));
		arrayReplacement.put("B", new String(Character.toChars(0xF201)));
		arrayReplacement.put("c", new String(Character.toChars(0xF202)));
		arrayReplacement.put("C", new String(Character.toChars(0xF202)));
		arrayReplacement.put("d", new String(Character.toChars(0xF203)));
		arrayReplacement.put("D", new String(Character.toChars(0xF203)));
		arrayReplacement.put("e", new String(Character.toChars(0xF204)));
		arrayReplacement.put("E", new String(Character.toChars(0xF204)));
		arrayReplacement.put("f", new String(Character.toChars(0xF205)));
		arrayReplacement.put("F", new String(Character.toChars(0xF205)));
		arrayReplacement.put("g", new String(Character.toChars(0xF206)));
		arrayReplacement.put("G", new String(Character.toChars(0xF206)));
		arrayReplacement.put("h", new String(Character.toChars(0xF207)));
		arrayReplacement.put("H", new String(Character.toChars(0xF207)));
		arrayReplacement.put("i", new String(Character.toChars(0xF208)));
		arrayReplacement.put("I", new String(Character.toChars(0xF208)));
		arrayReplacement.put("j", new String(Character.toChars(0xF209)));
		arrayReplacement.put("J", new String(Character.toChars(0xF209)));
		arrayReplacement.put("k", new String(Character.toChars(0xF20A)));
		arrayReplacement.put("K", new String(Character.toChars(0xF20A)));
		arrayReplacement.put("l", new String(Character.toChars(0xF20B)));
		arrayReplacement.put("L", new String(Character.toChars(0xF20B)));
		arrayReplacement.put("m", new String(Character.toChars(0xF20C)));
		arrayReplacement.put("M", new String(Character.toChars(0xF20C)));
		arrayReplacement.put("n", new String(Character.toChars(0xF20D)));
		arrayReplacement.put("N", new String(Character.toChars(0xF20D)));
		arrayReplacement.put("o", new String(Character.toChars(0xF20E)));
		arrayReplacement.put("O", new String(Character.toChars(0xF20E)));
		arrayReplacement.put("p", new String(Character.toChars(0xF20F)));
		arrayReplacement.put("P", new String(Character.toChars(0xF20F)));
		arrayReplacement.put("q", new String(Character.toChars(0xF210)));
		arrayReplacement.put("Q", new String(Character.toChars(0xF210)));
		arrayReplacement.put("r", new String(Character.toChars(0xF211)));
		arrayReplacement.put("R", new String(Character.toChars(0xF211)));
		arrayReplacement.put("s", new String(Character.toChars(0xF212)));
		arrayReplacement.put("S", new String(Character.toChars(0xF212)));
		arrayReplacement.put("t", new String(Character.toChars(0xF213)));
		arrayReplacement.put("T", new String(Character.toChars(0xF213)));
		arrayReplacement.put("u", new String(Character.toChars(0xF214)));
		arrayReplacement.put("U", new String(Character.toChars(0xF214)));
		arrayReplacement.put("v", new String(Character.toChars(0xF215)));
		arrayReplacement.put("V", new String(Character.toChars(0xF215)));
		arrayReplacement.put("w", new String(Character.toChars(0xF216)));
		arrayReplacement.put("W", new String(Character.toChars(0xF216)));
		arrayReplacement.put("x", new String(Character.toChars(0xF217)));
		arrayReplacement.put("X", new String(Character.toChars(0xF217)));
		arrayReplacement.put("y", new String(Character.toChars(0xF218)));
		arrayReplacement.put("Y", new String(Character.toChars(0xF218)));
		arrayReplacement.put("z", new String(Character.toChars(0xF219)));
		arrayReplacement.put("Z", new String(Character.toChars(0xF219)));
		//End of Erfan's English fix
		
		
		
	  arrayReplacement.put("।", "|");
	  arrayReplacement.put("‘", "Ô");
	  arrayReplacement.put("’", "Õ");
	  arrayReplacement.put("“", "Ò");
	  arrayReplacement.put("”", "Ó");
	  arrayReplacement.put("্র্য", "ª¨");
	  arrayReplacement.put("ম্প্র", "¤cÖ");
	  arrayReplacement.put("র‌্য", "i¨");
	  arrayReplacement.put("ক্ষ্ম", "²");
	  arrayReplacement.put("ক্ক", "°");
	  arrayReplacement.put("ক্ট", "±");
	  arrayReplacement.put("ক্ত", "³");
	  arrayReplacement.put("ক্ব", "K¡");
	  arrayReplacement.put("স্ক্র", "¯Œ");
	  arrayReplacement.put("ক্র", "µ");
	  arrayReplacement.put("ক্ল", "K¬");
	  arrayReplacement.put("ক্ষ", "¶");
	  arrayReplacement.put("ক্স", "·");
	  arrayReplacement.put("গু", "¸");
	  arrayReplacement.put("গ্ধ", "»");
	  arrayReplacement.put("গ্ন", "Mœ");
	  arrayReplacement.put("গ্ম", "M¥");
	  arrayReplacement.put("গ্ল", "M­");
	  arrayReplacement.put("গ্রু", "Mªy");
	  arrayReplacement.put("ঙ্ক", "¼");
	  arrayReplacement.put("ঙ্ক্ষ", "•¶");
	  arrayReplacement.put("ঙ্খ", "•L");
	  arrayReplacement.put("ঙ্গ", "½");
	  arrayReplacement.put("ঙ্ঘ", "•N");
	  arrayReplacement.put("চ্ছ্ব", "”Q¡");
	  arrayReplacement.put("চ্চ", "”P");
	  arrayReplacement.put("চ্ছ", "”Q");
	  arrayReplacement.put("চ্ঞ", "”T");
	  arrayReplacement.put("জ্জ্ব", "¾¡");
	  arrayReplacement.put("জ্জ", "¾");
	  arrayReplacement.put("জ্ঝ", "À");
	  arrayReplacement.put("জ্ঞ", "Á");
	  arrayReplacement.put("জ্ব", "R¡");
	  arrayReplacement.put("ঞ্চ", "Â");
	  arrayReplacement.put("ঞ্ছ", "Ã");
	  arrayReplacement.put("ঞ্জ", "Ä");
	  arrayReplacement.put("ঞ্ঝ", "Å");
	  arrayReplacement.put("ট্ট", "Æ");
	  arrayReplacement.put("ট্ব", "U¡");
	  arrayReplacement.put("ট্ম", "U¥");
	  arrayReplacement.put("ড্ড", "Ç");
	  arrayReplacement.put("ণ্ট", "È");
	  arrayReplacement.put("ণ্ঠ", "É");
	  arrayReplacement.put("ন্স", "Ý");
	  arrayReplacement.put("ণ্ড", "Ê");
	  arrayReplacement.put("ন্তু", "š‘");
	  arrayReplacement.put("ণ্ব", "Y^");
	  arrayReplacement.put("ত্ত্ব", "Ë¡");
	  arrayReplacement.put("ত্ত", "Ë");
	  arrayReplacement.put("ত্থ", "Ì");
	  arrayReplacement.put("ত্ন", "Zœ");
	  arrayReplacement.put("ত্ম", "Z¥");
	  arrayReplacement.put("ন্ত্ব", "š—¡");
	  arrayReplacement.put("ত্ব", "Z¡");
	  arrayReplacement.put("থ্ব", "_¡");
	  arrayReplacement.put("দ্গ", "˜M");
	  arrayReplacement.put("দ্ঘ", "˜N");
	  arrayReplacement.put("দ্দ", "Ï");
	  arrayReplacement.put("দ্ধ", "×");
	  arrayReplacement.put("দ্ব", "˜¡");
	  arrayReplacement.put("দ্ব", "Ø");
	  arrayReplacement.put("দ্ভ", "™¢");
	  arrayReplacement.put("দ্ম", "Ù");
	  arrayReplacement.put("দ্রু", "`ª“");
	  arrayReplacement.put("ধ্ব", "aŸ");
	  arrayReplacement.put("ধ্ম", "a¥");
	  arrayReplacement.put("ন্ট", "›U");
	  arrayReplacement.put("ন্ঠ", "Ú");
	  arrayReplacement.put("ন্ড", "Û");
	  arrayReplacement.put("ন্ত্র", "š¿");
	  arrayReplacement.put("ন্ত", "š—");
	  arrayReplacement.put("স্ত্র", "¯¿");
	  arrayReplacement.put("ত্র", "Î");
	  arrayReplacement.put("ন্থ", "š’");
	  arrayReplacement.put("ন্দ", "›`");
	  arrayReplacement.put("ন্দ্ব", "›Ø");
	  arrayReplacement.put("ন্ধ", "Ü");
	  arrayReplacement.put("ন্ন", "bœ");
	  arrayReplacement.put("ন্ব", "š^");
	  arrayReplacement.put("ন্ম", "b¥");
	  arrayReplacement.put("প্ট", "Þ");
	  arrayReplacement.put("প্ত", "ß");
	  arrayReplacement.put("প্ন", "cœ");
	  arrayReplacement.put("প্প", "à");
	  arrayReplacement.put("প্ল", "c­");
	  arrayReplacement.put("প্স", "á");
	  arrayReplacement.put("ফ্ল", "d¬");
	  arrayReplacement.put("ব্জ", "â");
	  arrayReplacement.put("ব্দ", "ã");
	  arrayReplacement.put("ব্ধ", "ä");
	  arrayReplacement.put("ব্ব", "eŸ");
	  arrayReplacement.put("ব্ল", "e¬");
	  arrayReplacement.put("ভ্র", "å");
	  arrayReplacement.put("ম্ন", "gœ");
	  arrayReplacement.put("ম্প", "¤ú");
	  arrayReplacement.put("ম্ফ", "ç");
	  arrayReplacement.put("ম্ব", "¤^");
	  arrayReplacement.put("ম্ভ", "¤¢");
	  arrayReplacement.put("ম্ভ্র", "¤£");
	  arrayReplacement.put("ম্ম", "¤§");
	  arrayReplacement.put("ম্ল", "¤­");
	  arrayReplacement.put("্র", "ª");
	  arrayReplacement.put("রু", "i“");
	  arrayReplacement.put("রূ", "iƒ");
	  arrayReplacement.put("ল্ক", "é");
	  arrayReplacement.put("ল্গ", "ê");
	  arrayReplacement.put("ল্ট", "ë");
	  arrayReplacement.put("ল্ড", "ì");
	  arrayReplacement.put("ল্প", "í");
	  arrayReplacement.put("ল্ফ", "î");
	  arrayReplacement.put("ল্ব", "j¦");
	  arrayReplacement.put("ল্ম", "j¥");
	  arrayReplacement.put("ল্ল", "j­");
	  arrayReplacement.put("শু", "ï");
	  arrayReplacement.put("শ্চ", "ð");
	  arrayReplacement.put("শ্ন", "kœ");
	  arrayReplacement.put("শ্ব", "k¦");
	  arrayReplacement.put("শ্ম", "k¥");
	  arrayReplacement.put("শ্ল", "k­");
	  arrayReplacement.put("ষ্ক", "®‹");
	  arrayReplacement.put("ষ্ক্র", "®Œ");
	  arrayReplacement.put("ষ্ট", "ó");
	  arrayReplacement.put("ষ্ঠ", "ô");
	  arrayReplacement.put("ষ্ণ", "ò");
	  arrayReplacement.put("ষ্প", "®ú");
	  arrayReplacement.put("ষ্ফ", "õ");
	  arrayReplacement.put("ষ্ম", "®§");
	  arrayReplacement.put("স্ক", "¯‹");
	  arrayReplacement.put("স্ট", "÷");
	  arrayReplacement.put("স্খ", "ö");
	  arrayReplacement.put("স্ত", "¯—");
	  arrayReplacement.put("স্তু", "¯‘");
	  arrayReplacement.put("স্থ", "¯’");
	  arrayReplacement.put("স্ন", "mœ");
	  arrayReplacement.put("স্প", "¯ú");
	  arrayReplacement.put("স্ফ", "ù");
	  arrayReplacement.put("স্ব", "¯^");
	  arrayReplacement.put("স্ম", "¯§");
	  arrayReplacement.put("স্ল", "¯­");
	  arrayReplacement.put("হু", "û");
	  arrayReplacement.put("হ্ণ", "nè");
	  arrayReplacement.put("হ্ব", "nŸ");
	  arrayReplacement.put("হ্ন", "ý");
	  arrayReplacement.put("হ্ম", "þ");
	  arrayReplacement.put("হ্ল", "n¬");
	  arrayReplacement.put("হৃ", "ü");
	  arrayReplacement.put("র্", "©");
	  arrayReplacement.put("্র", "«");
	  arrayReplacement.put("্য", "¨");
	  arrayReplacement.put("্", "&");
	  arrayReplacement.put("আ", "Av");
	  arrayReplacement.put("অ", "A");
	  arrayReplacement.put("ই", "B");
	  arrayReplacement.put("ঈ", "C");
	  arrayReplacement.put("উ", "D");
	  arrayReplacement.put("ঊ", "E");
	  arrayReplacement.put("ঋ", "F");
	  arrayReplacement.put("এ", "G");
	  arrayReplacement.put("ঐ", "H");
	  arrayReplacement.put("ও", "I");
	  arrayReplacement.put("ঔ", "J");
	  arrayReplacement.put("ক", "K");
	  arrayReplacement.put("খ", "L");
	  arrayReplacement.put("গ", "M");
	  arrayReplacement.put("ঘ", "N");
	  arrayReplacement.put("ঙ", "O");
	  arrayReplacement.put("চ", "P");
	  arrayReplacement.put("ছ", "Q");
	  arrayReplacement.put("জ", "R");
	  arrayReplacement.put("ঝ", "S");
	  arrayReplacement.put("ঞ", "T");
	  arrayReplacement.put("ট", "U");
	  arrayReplacement.put("ঠ", "V");
	  arrayReplacement.put("ড", "W");
	  arrayReplacement.put("ঢ", "X");
	  arrayReplacement.put("ণ", "Y");
	  arrayReplacement.put("ত", "Z");
	  arrayReplacement.put("থ", "_");
	  arrayReplacement.put("দ", "`");
	  arrayReplacement.put("ধ", "a");
	  arrayReplacement.put("ন", "b");
	  arrayReplacement.put("প", "c");
	  arrayReplacement.put("ফ", "d");
	  arrayReplacement.put("ব", "e");
	  arrayReplacement.put("ভ", "f");
	  arrayReplacement.put("ম", "g");
	  arrayReplacement.put("য", "h");
	  arrayReplacement.put("র", "i");
	  arrayReplacement.put("ল", "j");
	  arrayReplacement.put("শ", "k");
	  arrayReplacement.put("ষ", "l");
	  arrayReplacement.put("স", "m");
	  arrayReplacement.put("হ", "n");
	  arrayReplacement.put("ড়", "o");
	  arrayReplacement.put("ঢ়", "p");
	  arrayReplacement.put("য়", "q");
	  arrayReplacement.put("ৎ", "r");
	  arrayReplacement.put("০", "0");
	  arrayReplacement.put("১", "1");
	  arrayReplacement.put("২", "2");
	  arrayReplacement.put("৩", "3");
	  arrayReplacement.put("৪", "4");
	  arrayReplacement.put("৫", "5");
	  arrayReplacement.put("৬", "6");
	  arrayReplacement.put("৭", "7");
	  arrayReplacement.put("৮", "8");
	  arrayReplacement.put("৯", "9");
	  arrayReplacement.put("া", "v");
	  arrayReplacement.put("ি", "w");
	  arrayReplacement.put("ী", "x");
	  arrayReplacement.put("ু", "y");
	  arrayReplacement.put("ূ", "~");
	  arrayReplacement.put("ৃ", "…");
	  arrayReplacement.put("ে", "‡");
	  arrayReplacement.put("ৈ", "‰");
	  arrayReplacement.put("ৗ", "Š");
	  arrayReplacement.put("ং", "s");
	  arrayReplacement.put("ঃ", "t");
	  arrayReplacement.put("ঁ", "u");
	}
	
	static boolean D(char f) {
	    if (f == '্') return true;
	    return false;
	}
	
	static boolean v(char f) {
	    if (f == 'ক' || f == 'খ' || f == 'গ' || f == 'ঘ' || f == 'ঙ' || f == 'চ' || f == 'ছ' || f == 'জ' || f == 'ঝ' || f == 'ঞ' || f == 'ট' || f == 'ঠ' || f == 'ড' || f == 'ঢ' || f == 'ণ' || f == 'ত' || f == 'থ' || f == 'দ' || f == 'ধ' || f == 'ন' || f == 'প' || f == 'ফ' || f == 'ব' || f == 'ভ' || f == 'ম' || f == 'শ' || f == 'ষ' || f == 'স' || f == 'হ' || f == 'য' || f == 'র' || f == 'ল' || f == 'য়' || f == 'ং' || f == 'ঃ' || f == 'ঁ' || f == 'ৎ') return true;
	    return false;
	}
	
	static boolean ao(char f) {
	    if (f == 'ি' || f == 'ৈ' || f == 'ে') return true;
	    return false;
	}
	
	static String  ci(String w) {
	    int cY = 0;
	    for (int i = 0; i < w.length(); i++) {
	        if (i < w.length() && ao(w.charAt(i))) {
	            int j = 1;
	            while (v(w.charAt(i - j))) {
	                if (i - j < 0) break;
	                if (i - j <= cY) break;
	                if (D(w.charAt(i - j - 1))) j += 2;
	                else break;
	            }
	            
	            String R = w.substring(0, i - j);
	            R += w.charAt(i);
	            R += w.substring(i - j, i);
	            R += w.substring(i + 1, w.length());
	            w = R;
	            cY = i + 1;
	            continue;
	        }
	        if (i < w.length() - 1 && D(w.charAt(i)) && w.charAt(i - 1) == 'র' && !D(w.charAt(i - 2))) {
	            int j = 1;
	            int aZ = 0;
	            while (true) {
	            	try{
		                if ((i+j)<w.length() && v(w.charAt(i + j)) && (i+j+1)<w.length() && D(w.charAt(i + j + 1))){ 
		                	j += 2;
		                }
		                else if ((i+j)<w.length() && v(w.charAt(i + j)) && (i+j+1)<w.length() && ao(w.charAt(i + j + 1))) {
		                    aZ = 1;
		                    break;
		                } 
		                else {
		                	break;	            		
		                }
	            	}
	            	catch (StringIndexOutOfBoundsException e) {
	            		//break;
					}

	            }
	            String R = w.substring(0, i - 1);
	            R += w.substring(i + j + 1, i + j + aZ + 1);
	            R += w.substring(i + 1, i + j + 1);
	            R += w.charAt(i - 1);
	            R += w.charAt(i);
	            R += w.substring(i + j + aZ + 1, w.length());
	            w = R;
	            i += (j + aZ);
	            cY = i + 1;
	            continue;
	        }
	    }
	    return w;
	}
	
	
	public static String UnicodeToAnsi(String input){
		
		
		
		
		//Erfan's additional fixes
		input = input.replace("ড়", "ড়");
		input = input.replace("য়", "য়");
		input = input.replace("ঢ়", "ঢ়");
		input = input.replace("ব়", "র");
		input = input.replace("৷", "|");
		//End of Erfan's additional fixes
		
		input = input.replace("ো", "ো");
		input = input.replace("ৌ", "ৌ");
		try{
			input = ci(input);
		}
		catch (Exception e) {
		}
		
		Set<Entry<String, String>> entrySet = arrayReplacement.entrySet();
		for (Entry<String, String> entry : entrySet) {
			input = input.replace(entry.getKey(), entry.getValue());
		}
		return  input;
	}
	
}
